package com.tengju.data.domain.model.user;

import com.tengju.user.domain.model.club.ClubId;
import lombok.Value;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;

@Value
public class DominationDetail {

    UserIdCode idCode;

    StarLevelEnum starLevel;

    DominationLevelEnum dominationLevel;

    UserIdCode parentIdCode;

    StarLevelEnum parentStarLevel;

    BigDecimal currentMonthAchievement;

    Integer dominationEcNum;

    Integer dominationCityStarOneNum;

    Integer dominationCityStarTwoNum;

    Integer dominationCityStarThreeNum;

    Integer dominationCityStarFourNum;

    Integer dominationDealerNum;

    LocalDateTime registerTime;

    Integer userLevelFourCnt;

    Integer userLevelThreeCnt;

    Integer userLevelTwoCnt;

    public Integer serviceProviderNum() {
        return Optional.ofNullable(dominationCityStarOneNum).orElse(0) +
                Optional.ofNullable(dominationCityStarTwoNum).orElse(0)  +
                Optional.ofNullable(dominationCityStarThreeNum).orElse(0) +
                Optional.ofNullable(dominationCityStarFourNum).orElse(0);
    }

    public String registerTimeStr() {
        if (registerTime == null) {
            return "";
        } else {
            return registerTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        }
    }

    public Map<StarRoleEnum, Map<StarLevelEnum, Integer>> getDominationTeamMemberNum() {

        Map<StarRoleEnum, Map<StarLevelEnum, Integer>> memberNumDetail = new LinkedHashMap<>();
        Map<StarLevelEnum, Integer> operatorMemberNum = new LinkedHashMap<>();
        Map<StarLevelEnum, Integer> serviceProviderMemberNum = new LinkedHashMap<>();
        Map<StarLevelEnum, Integer> dealerMemberNum = new LinkedHashMap<>();
        switch (starLevel) {
            case EEC:
            case SEC:
                //运营商
                operatorMemberNum.put(StarLevelEnum.EC, dominationEcNum);
                memberNumDetail.put(StarRoleEnum.OPERATOR, operatorMemberNum);
                //服务商
                serviceProviderMemberNum.put(StarLevelEnum.CITY_STAR_ONE, dominationCityStarOneNum);
                serviceProviderMemberNum.put(StarLevelEnum.CITY_STAR_TWO, dominationCityStarTwoNum);
                serviceProviderMemberNum.put(StarLevelEnum.CITY_STAR_THREE, dominationCityStarThreeNum);
                serviceProviderMemberNum.put(StarLevelEnum.CITY_STAR_FOUR, dominationCityStarFourNum);
                memberNumDetail.put(StarRoleEnum.SERVICE_PROVIDER, serviceProviderMemberNum);
                //经销商
                dealerMemberNum.put(StarLevelEnum.NONE, dominationDealerNum);
                memberNumDetail.put(StarRoleEnum.NONE, dealerMemberNum);
                return memberNumDetail;
            case EC:
                //服务商
                serviceProviderMemberNum.put(StarLevelEnum.CITY_STAR_ONE, dominationCityStarOneNum);
                serviceProviderMemberNum.put(StarLevelEnum.CITY_STAR_TWO, dominationCityStarTwoNum);
                serviceProviderMemberNum.put(StarLevelEnum.CITY_STAR_THREE, dominationCityStarThreeNum);
                serviceProviderMemberNum.put(StarLevelEnum.CITY_STAR_FOUR, dominationCityStarFourNum);
                memberNumDetail.put(StarRoleEnum.SERVICE_PROVIDER, serviceProviderMemberNum);
                //经销商
                dealerMemberNum.put(StarLevelEnum.NONE, dominationDealerNum);
                memberNumDetail.put(StarRoleEnum.NONE, dealerMemberNum);
                return memberNumDetail;
            case CITY_STAR_ONE:
            case CITY_STAR_TWO:
            case CITY_STAR_THREE:
            case CITY_STAR_FOUR:
                //经销商
                dealerMemberNum.put(StarLevelEnum.NONE, dominationDealerNum);
                memberNumDetail.put(StarRoleEnum.NONE, dealerMemberNum);
                return memberNumDetail;
            default:
                return memberNumDetail;
        }
    }

    public Map<StarLevelEnum, Integer> getDominationTeamAllMemberNum() {
        Map<StarLevelEnum, Integer> memberNumDetail = new LinkedHashMap<>();
        if (StarLevelEnum.EEC == starLevel || StarLevelEnum.SEC == starLevel) {
            //经销商
            memberNumDetail.put(StarLevelEnum.NONE, dominationDealerNum);
            //服务商
            memberNumDetail.put(StarLevelEnum.CITY_STAR_ONE, dominationCityStarOneNum);
            memberNumDetail.put(StarLevelEnum.CITY_STAR_TWO, dominationCityStarTwoNum);
            memberNumDetail.put(StarLevelEnum.CITY_STAR_THREE, dominationCityStarThreeNum);
            memberNumDetail.put(StarLevelEnum.CITY_STAR_FOUR, dominationCityStarFourNum);
            //运营商
            memberNumDetail.put(StarLevelEnum.EC, dominationEcNum);
        } else if (StarLevelEnum.EC == starLevel) {
            //经销商
            memberNumDetail.put(StarLevelEnum.NONE, dominationDealerNum);
            //服务商
            memberNumDetail.put(StarLevelEnum.CITY_STAR_ONE, dominationCityStarOneNum);
            memberNumDetail.put(StarLevelEnum.CITY_STAR_TWO, dominationCityStarTwoNum);
            memberNumDetail.put(StarLevelEnum.CITY_STAR_THREE, dominationCityStarThreeNum);
            memberNumDetail.put(StarLevelEnum.CITY_STAR_FOUR, dominationCityStarFourNum);
        } else if (StarLevelEnum.CITY_STAR_ONE == starLevel ||
                StarLevelEnum.CITY_STAR_TWO == starLevel ||
                StarLevelEnum.CITY_STAR_THREE == starLevel ||
                StarLevelEnum.CITY_STAR_FOUR == starLevel) {
            //经销商
            memberNumDetail.put(StarLevelEnum.NONE, dominationDealerNum);
        }
        return memberNumDetail;
    }
}
